﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace TomVanGaever.BCS.Parser
{
	public partial class BCSParserEntityPropertyPickerControl : BCSUserControl
	{

		public IEnumerable<PropertyInfo> DataSource { get; set; }
		public Type SelectedEntityType { get; set; }
		public string TypeMember { get; set; }

		private Func<Type> GetSelectedType;
		private Func<PropertyInfo> GetSelectedIdentifier;


		public void GetSelectedEntityType(Func<Type> action)
		{
			this.GetSelectedType = action;
		}
		public void GetSelectedEntityIdentifier(Func<PropertyInfo> action)
		{
			this.GetSelectedIdentifier = action;
		}

		public override string TitleText
		{
			get
			{
				label2.Visible = false;
				return label2.Text;
			}
		}

		public void DataBind()
		{
			lstIdentifierProperties.DisplayMember = this.TypeMember;
			lstIdentifierProperties.DataSource = DataSource;
		}

		public BCSParserEntityPropertyPickerControl()
		{
			InitializeComponent();
		}

		public override string GetStatusString()
		{
			string result = "Selected properties this entity:\t\t\t\t {0}";
			string scoll = string.Empty;
			foreach (var t in this.GetSelectedEntityProperties())
			{
				scoll += t.Name + ",";
			}
			return string.Format(result, scoll);
		}

		private void BCSParserEntityIdentifierPickerControl_Load(object sender, EventArgs e)
		{
			this.SelectedEntityType = this.GetSelectedType.Invoke();
			this.TypeMember = "Name";
			this.DataSource = this.SelectedEntityType.GetProperties().ToList();
			this.DataBind();

			PropertyInfo identifier = this.GetSelectedIdentifier.Invoke();
			SetIdentifier(identifier);
		}

		public IEnumerable<PropertyInfo> GetSelectedEntityProperties()
		{
			var coll = lstIdentifierProperties.CheckedItems.Cast<PropertyInfo>().ToList();
			return coll;
		}


		private void lstIdentifierProperties_SelectedIndexChanged(object sender, EventArgs e)
		{
			bool enabled = lstIdentifierProperties.CheckedItems.Count != 0;
			ParserForm.NextButton.Enabled = enabled;
		}

		private void SetIdentifier(PropertyInfo identifier)
		{
			for (int i = 0; i < lstIdentifierProperties.Items.Count; i++)
			{
				PropertyInfo checkitem = (PropertyInfo)lstIdentifierProperties.Items[i];
				if (identifier.Name == checkitem.Name)
				{
					lstIdentifierProperties.SetItemCheckState(i, CheckState.Indeterminate);
				}
			}
		}

		private void btnAll_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < lstIdentifierProperties.Items.Count; i++)
			{
				lstIdentifierProperties.SetItemCheckState(i, CheckState.Checked);
			}
			lstIdentifierProperties_SelectedIndexChanged(null, null);
		}

		private void btnToggle_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < lstIdentifierProperties.Items.Count; i++)
			{

				if (lstIdentifierProperties.GetItemChecked(i))
				{
					lstIdentifierProperties.SetItemCheckState(i, CheckState.Unchecked);
				}
				else
				{
					lstIdentifierProperties.SetItemCheckState(i, CheckState.Checked);
				}
			}
			lstIdentifierProperties_SelectedIndexChanged(null, null);
		}
	}
}
